package com.finance.framework.lang;


/**
 * Created by CBWL on 2017/4/28.
 */
public enum Privilege {

    B1(1L),//direction
    B2(2L),//temperature
    B3(4L),//importsource
    B4(8L),//supplier
    B5(16L),//postman
    B6(32L),//diliverysite
    B7(64L),//acceptorg
    B8(128L),//status
    B9(256L),//scheduleType
    B10(512L),//consignee address
    B11(1024L),
    B12(2048L),
    B13(4069L),
    B14(8192L),
    B15(16384L),
    B16(32768L),
    B17(65536L),
    B18(131072L),
    B19(262144L);



    Long num;
    public static final Long ALL_PRIVILEGE = B1.num|B2.num|B3.num|B4.num|B5.num|B6.num|B7.num|B8.num|B9.num|B10.num|B11.num|B12.num|B13.num|B14.num|B15.num|B16.num|B17.num|B18.num|B19.num;

    public String getPrivilegeString(){
        return this.num + "";
    }

    public Long getPrivilegeValue(){
        return num;
    }

    Privilege(Long num){
        this.num = num;
    }

    public static Privilege valueOf(Long privilege){
        Privilege[] privileges = Privilege.values();
        for(Privilege pv : privileges){
            if(pv.num.equals(privilege)) {
                return pv;
            }
        }
        return null;
    }

    public static boolean has(Long privileges, Privilege privilege){
        return (privileges & privilege.num) == privilege.num;
    }

    public static Long grant(Long privileges, Privilege privilege){
        if(!has(privileges, privilege)) {
            return privileges | privilege.num;
        }
        return privileges;
    }

    public static Long unGrant(Long privileges, Privilege privilege){
        if(!has(privileges,privilege)) {
            return privileges;
        }
        return privileges^privilege.num;
    }
 }
